package com.edu.strDemo;

import cn.hutool.core.date.DateTime;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.edu.pojo.RedisKey;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author five-five
 * @version 1.0
 * todo：
 * @date 2021/7/12 21:04
 */
public class TestString02 {
    private static final String header = "'";
    private static final String foot = "'";


    public static void main(String[] args) {
        TestString02 testString01 = new TestString02();
        System.out.println(MessageString.messageStr);
        Map<String, String> map = testString01.parsingStr(MessageString.messageStr);
        System.out.println(map);
    }

    /**
     * 解析报文
     * 第一步：&}
     * 第二步：&,
     * 第三步："	"
     *
     * @param message 报文
     * @return 解析后的报文
     */
    public Map parsingStr(String message) {
        List<RedisKey> redisKeyList = new ArrayList<>();
        String[] strings = message.substring(message.indexOf("{") + 1, message.indexOf("&}")).split("&,");
        for (String string : strings) {
            String str = string.substring(0, string.lastIndexOf("0")).trim();
            str.intern();//放入常量池
            String redisKey = str.substring(0, str.indexOf("|"));
            redisKey = redisKey.endsWith("#") ? redisKey.substring(0, str.indexOf("#")) : redisKey;
            System.out.println("" + redisKey);
            //取出time头
            String time = str.substring(str.indexOf("|") + 1, str.indexOf("="));
            str = str.substring(str.indexOf("=") + 1);
            //增加'head
            str = "'" + str.replace("=", "'='");
            //增加'foot
            str = str.replace(",", "','");
            str = str + "'";
            //把=转变为:
            str = str.replace("=", ":");
            //增加time头
            str = "'time':'" + time + "'," + str;
            System.out.println("{" + str + "}");
//            Jedis jedis = new Jedis("192.168.0.48");
//            jedis.set("zhts_MY3.0" + redisKey, "{" + str + "}");
            JSONObject jsonObject = JSONObject.parseObject("{" + str + "}");
            String fdl = "D";
            String rfdl = "E";
            String fx = "BK";
            String fs = "BH";
            if (redisKey.equalsIgnoreCase("dq")) {
                continue;
            }
            RedisKey redisKey1 = new RedisKey();
            redisKey1.setFj(redisKey);
            redisKey1.setFc("zhts_MY3.0");
            redisKey1.setFdl(jsonObject.getString(fdl));
            redisKey1.setFs(jsonObject.getString(fs));
            redisKey1.setFx(jsonObject.getString(fx));
            redisKey1.setRfdl(jsonObject.getString(rfdl));
            redisKeyList.add(redisKey1);
        }
//        Jedis jedis = new Jedis("192.168.0.48");
//        jedis.set("ts_ts", JSONObject.toJSONString(redisKeyList));
        return null;
    }
}